# usage: python /home/orsi/brahmsProject/analysisCode/zConstraint/computeAveragesAndStdErrorsZconstraintData_10sets.py

import sys,string,math

nSets = 10 # number of sets/samples

# Avg and SE for vals from force.dat
f1 = open('analysisSet1/force.dat','r')
f2 = open('analysisSet2/force.dat','r')
f3 = open('analysisSet3/force.dat','r')
f4 = open('analysisSet4/force.dat','r')
f5 = open('analysisSet5/force.dat','r')
f6 = open('analysisSet6/force.dat','r')
f7 = open('analysisSet7/force.dat','r')
f8 = open('analysisSet8/force.dat','r')
f9 = open('analysisSet9/force.dat','r')
f10 = open('analysisSet10/force.dat','r')

o = open('force.dat','w') # Write file with all three values of the constraint force.
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
        line5 = f5.readline()
        line6 = f6.readline()
        line7 = f7.readline()
        line8 = f8.readline()
        line9 = f9.readline()
        line10 = f10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
        if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1], words5[1], words6[1], words7[1], words8[1] , words9[1] , words10[1] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
f7.close()
f8.close()
f9.close()
f10.close()
o.close()

f = open('force.dat','r')
o = open('avgForce.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]);
	force1 = float(val[1]);
	force2 = float(val[2]);
	force3 = float(val[3]);
	force4 = float(val[4]);
	force5 = float(val[5]);
	force6 = float(val[6]);
	force7 = float(val[7]);
	force8 = float(val[8]);
	force9 = float(val[9]);
	force10 = float(val[10]);
	sumy = force1 + force2 + force3 + force4 + force5 + force6 + force7 + force8 + force9 + force10 
	#print >>o, sumy
	avgy = float(sumy/nSets) # Avgerage values.
	#av = float((a-avgy)**2) #Calculate the square of the distance from the mean for each value
	#bv = float((b-avgy)**2)
	#cv = float((c-avgy)**2)
	sqForce1 = float((force1)**(2))
	sqForce2 = float((force2)**(2))
	sqForce3 = float((force3)**(2))
	sqForce4 = float((force4)**(2))
	sqForce5 = float((force5)**(2))
	sqForce6 = float((force6)**(2))
	sqForce7 = float((force7)**(2))
	sqForce8 = float((force8)**(2))
	sqForce9 = float((force9)**(2))
	sqForce10 = float((force10)**(2))
	sum2 = float((sqForce1+sqForce2+sqForce3+sqForce4+sqForce5+sqForce6+sqForce7+sqForce8+sqForce9+sqForce10)/nSets)
	sumsq = float(((force1+force2+force3+force4+force5+force6+force7+force8+force9+force10)/nSets)**(2))
	#sdy = float(((av+bv+cv)/3)**(1/2.0))
	sdy = float((sum2-sumsq)**(1/2.0))
	sey = float(sdy/(nSets**0.5))
	print >>o, x,"\t",avgy,"\t",sey
	#print >>o, x,"\t",round(avgy,3)," \t",round(sey,3) Rounding - if req.
f.close()
o.close()

# Avg vals and SE for diffusion coefficient.
d1 = open('analysisSet1/D.dat','r')
d2 = open('analysisSet2/D.dat','r')
d3 = open('analysisSet3/D.dat','r')
d4 = open('analysisSet4/D.dat','r')
d5 = open('analysisSet5/D.dat','r')
d6 = open('analysisSet6/D.dat','r')
d7 = open('analysisSet7/D.dat','r')
d8 = open('analysisSet8/D.dat','r')
d9 = open('analysisSet8/D.dat','r')
d10 = open('analysisSet8/D.dat','r')
o = open('D.dat','w')
while 1:
        line1 = d1.readline() # Read each line - data MUST begin at line 0.
        line2 = d2.readline()
	line3 = d3.readline()
        line4 = d4.readline()
        line5 = d5.readline()
        line6 = d6.readline()
        line7 = d7.readline()
        line8 = d8.readline()
        line9 = d9.readline()
        line10 = d10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
        if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1], words5[1], words6[1], words7[1], words8[1], words9[1], words10[1]  # Print each value
d1.close()
d2.close()
d3.close()
d4.close()
d5.close()
d6.close()
d7.close()
d8.close()
d9.close()
d10.close()
o.close()

f = open('D.dat','r')
o = open('avgD.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]);
	diff1 = float(val[1]);
	diff2 = float(val[2]);
	diff3 = float(val[3]);
	diff4 = float(val[4]);
	diff5 = float(val[5]);
	diff6 = float(val[6]);
	diff7 = float(val[7]);
	diff8 = float(val[8]);
	diff9 = float(val[9]);
	diff10 = float(val[10]);
	sumy = diff1 + diff2 + diff3 + diff4 + diff5 + diff6 + diff7 + diff8 + diff9 + diff10  
	avgy = float(sumy/nSets) # Avgerage values.
	sqDiff1 = float((diff1)**(2))
	sqDiff2 = float((diff2)**(2))
	sqDiff3 = float((diff3)**(2))
	sqDiff4 = float((diff4)**(2))
	sqDiff5 = float((diff5)**(2))
	sqDiff6 = float((diff6)**(2))
	sqDiff7 = float((diff7)**(2))
	sqDiff8 = float((diff8)**(2))
	sqDiff9 = float((diff9)**(2))
	sqDiff10 = float((diff10)**(2))
	sum2 = float((sqDiff1+sqDiff2+sqDiff3+sqDiff4+sqDiff5+sqDiff6+sqDiff7+sqDiff8+sqDiff9+sqDiff10)/nSets)
	sumsq = float(((diff1+diff2+diff3+diff4+diff5+diff6+diff7+diff8+diff9+diff10)/nSets)**(2))
	sdy = float((sum2-sumsq)**(1/2.0))
	sey = float(sdy/(nSets**0.5))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Avg vals and SE for free energy.
g1 = open('analysisSet1/G.dat','r')
g2 = open('analysisSet2/G.dat','r')
g3 = open('analysisSet3/G.dat','r')
g4 = open('analysisSet4/G.dat','r')
g5 = open('analysisSet5/G.dat','r')
g6 = open('analysisSet6/G.dat','r')
g7 = open('analysisSet7/G.dat','r')
g8 = open('analysisSet8/G.dat','r')
g9 = open('analysisSet9/G.dat','r')
g10 = open('analysisSet10/G.dat','r')
o = open('G.dat','w')
while 1:
        line1 = g1.readline() # Read each line - data MUST begin at line 0.
        line2 = g2.readline()
	line3 = g3.readline()
        line4 = g4.readline()
        line5 = g5.readline()
        line6 = g6.readline()
        line7 = g7.readline()
        line8 = g8.readline()
        line9 = g9.readline()
        line10 = g10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
        if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1], words5[1], words6[1], words7[1], words8[1] , words9[1] , words10[1]  # Print each value
g1.close()
g2.close()
g3.close()
g4.close()
g5.close()
g6.close()
g7.close()
g8.close()
g9.close()
g10.close()
o.close()

f = open('G.dat','r')
o = open('avgG.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]); 
	deltaG1 = float(val[1]);
	deltaG2 = float(val[2]);
	deltaG3 = float(val[3]);
	deltaG4 = float(val[4]);
	deltaG5 = float(val[5]);
	deltaG6 = float(val[6]);
	deltaG7 = float(val[7]);
	deltaG8 = float(val[8]);
	deltaG9 = float(val[9]);
	deltaG10 = float(val[10]);
	sumy = deltaG1 + deltaG2 + deltaG3 + deltaG4 + deltaG5 +deltaG6 +deltaG7 +deltaG8 +deltaG9 +deltaG10 
	avgy = float(sumy/nSets) # Avgerage values.
	sqDeltaG1 = float((deltaG1)**(2))
	sqDeltaG2 = float((deltaG2)**(2))
	sqDeltaG3 = float((deltaG3)**(2))
	sqDeltaG4 = float((deltaG4)**(2))
	sqDeltaG5 = float((deltaG5)**(2))
	sqDeltaG6 = float((deltaG6)**(2))
	sqDeltaG7 = float((deltaG7)**(2))
	sqDeltaG8 = float((deltaG8)**(2))
	sqDeltaG9 = float((deltaG9)**(2))
	sqDeltaG10 = float((deltaG10)**(2))
	sum2 = float((sqDeltaG1+sqDeltaG2+sqDeltaG3+sqDeltaG4+sqDeltaG5+sqDeltaG6+sqDeltaG7+sqDeltaG8+sqDeltaG9+sqDeltaG10)/nSets)
	sumsq = float(((deltaG1+deltaG2+deltaG3+deltaG4+deltaG5+deltaG6+deltaG7+deltaG8+deltaG9+deltaG10)/nSets)**(2))
	sdy = float((sum2-sumsq)**(1/2.0))
	sey = float(sdy/(nSets**0.5))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Avg vals and SE for resistance.
r1 = open('analysisSet1/R.dat','r')
r2 = open('analysisSet2/R.dat','r')
r3 = open('analysisSet3/R.dat','r')
r4 = open('analysisSet4/R.dat','r')
r5 = open('analysisSet5/R.dat','r')
r6 = open('analysisSet6/R.dat','r')
r7 = open('analysisSet7/R.dat','r')
r8 = open('analysisSet8/R.dat','r')
r9 = open('analysisSet9/R.dat','r')
r10 = open('analysisSet10/R.dat','r')
o = open('R.dat','w')
while 1:
        line1 = r1.readline() # Read each line - data MUST begin at line 0.
        line2 = r2.readline()
	line3 = r3.readline()
        line4 = r4.readline()
        line5 = r5.readline()
        line6 = r6.readline()
        line7 = r7.readline()
        line8 = r8.readline()
        line9 = r9.readline()
        line10 = r10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
        if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1], words5[1], words6[1], words7[1], words8[1], words9[1], words10[1]  # Print each value
r1.close()
r2.close()
r3.close()
r4.close()
r5.close()
r6.close()
r7.close()
r8.close()
r9.close()
r10.close()
o.close()

f = open('R.dat','r')
o = open('avgR.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]);
	res1 = float(val[1]);
	res2 = float(val[2]);
	res3 = float(val[3]);
	res4 = float(val[4]);
	res5 = float(val[5]);
	res6 = float(val[6]);
	res7 = float(val[7]);
	res8 = float(val[8]);
	res9 = float(val[9]);
	res10 = float(val[10]);
	sumy = res1 + res2 + res3 + res4 + res5  + res6  + res7  + res8  + res9  + res10  
	avgy = float(sumy/nSets) # Avgerage values.
	sqRes1 = float((res1)**(2))
	sqRes2 = float((res2)**(2))
	sqRes3 = float((res3)**(2))
	sqRes4 = float((res4)**(2))
	sqRes5 = float((res5)**(2))
	sqRes6 = float((res6)**(2))
	sqRes7 = float((res7)**(2))
	sqRes8 = float((res8)**(2))
	sqRes9 = float((res9)**(2))
	sqRes10 = float((res10)**(2))
	sum2 = float((sqRes1+sqRes2+sqRes3+sqRes4+sqRes5+sqRes6+sqRes7+sqRes8+sqRes9+sqRes10)/nSets)
	sumsq = float(((res1+res2+res3+res4+res5+res6+res7+res8+res9+res10)/nSets)**(2))
	sdy = float((sum2-sumsq)**(1/2.0))
	sey = float(sdy/(nSets**0.5))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Calculate average vals and SE for the permeability coefficient.
f = open('analysisSet1/P','r')
o = open('avgP.dat','w')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p1 = float(words[4])
f.close()
f = open('analysisSet2/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p2 = float(words[4])
f.close()
f = open('analysisSet3/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p3 = float(words[4])
f.close()
f = open('analysisSet4/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p4 = float(words[4])
f.close()
f = open('analysisSet5/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p5 = float(words[4])
f.close()
f = open('analysisSet6/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p6 = float(words[4])
f.close()
f = open('analysisSet7/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p7 = float(words[4])
f.close()
f = open('analysisSet8/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p8 = float(words[4])
f.close()
f = open('analysisSet9/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p9 = float(words[4])
f.close()
f = open('analysisSet10/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p10 = float(words[4])
f.close()

sum = p1 + p2 + p3 + p4 +p5 +p6+p7+p8+p9+p10
avg = sum/nSets
sqP1 = float((p1)**(2))
sqP2 = float((p2)**(2))
sqP3 = float((p3)**(2))
sqP4 = float((p4)**(2))
sqP5 = float((p5)**(2))
sqP6 = float((p6)**(2))
sqP7 = float((p7)**(2))
sqP8 = float((p8)**(2))
sqP9 = float((p9)**(2))
sqP10 = float((p10)**(2))
sum2 = float((sqP1+sqP2+sqP3+sqP4+sqP5+sqP6+sqP7+sqP8+sqP9+sqP10)/nSets)
sumsq = float(((p1+p2+p3+p4+p5+p6+p7+p8+p9+p10)/nSets)**(2))
sdy = float((sum2-sumsq)**(1/2.0))
sey = float(sdy/(nSets**0.5))
print >>o, "%8.3e"% avg,"%8.3e"% sey
f.close()
o.close()



